<template>
  <h1>发微博页面</h1>
  <el-input v-model="w.content" placeholder="说点儿啥..."></el-input>
  <el-button @click="post()">发布微博</el-button>
</template>
<script setup>
  import {ref} from "vue";
  import qs from 'qs';
  import axios from "axios";
  import router from "@/router";
  import {ElMessage} from "element-plus";
  const user = localStorage.user?JSON.parse(localStorage.user):null;
  //和页面进行绑定的变量需要定义成响应式变量
  const w = ref({content:"",userId:user.id});
  const post = ()=>{
    //把对象转成查询字符串
    let data = qs.stringify(w.value);
    axios.post('http://localhost:8080/v1/weibos/add-new',data)
        .then((response)=>{
        if (response.data.code==2001){
          router.push('/');
        }else{
          ElMessage.error(response.data.msg);
        }
    })
  }
</script>

<style scoped>

</style>